import { Store, StoreHandler } from "../../core";

import StoreProps from "./StoreProps";
import { useEffect, useState } from "react";

const useRotation = (store: Store<StoreProps>): { rotation: number } => {
  const [rotation, setRotation] = useState(store.get("rotation") || 0);

  const handleRotationChanged: StoreHandler<number> = (
    currentRotation: number
  ) => setRotation(currentRotation);

  useEffect(() => {
    store.subscribe("rotation", handleRotationChanged);

    return () => {
      store.unsubscribe("rotation", handleRotationChanged);
    };
  }, []);

  return { rotation };
};

export default useRotation;
